package routers

import (
	v1 "ginBlog/api/v1"
	"ginBlog/middleware"
	"ginBlog/utils"
	"github.com/gin-gonic/gin"
)

func InitRouter() {
	gin.SetMode(utils.AppMode)
	r := gin.New()
	// 引入日志
	r.Use(middleware.Log())
	// 处理引入的中间件
	r.Use(gin.Recovery())
	// 引入跨域处理
	r.Use(middleware.Cors())

	// 配置静态文件,第一个参数(api)路由路径，第二个静态的文件夹相对目录
	r.Static("/static", "./static")

	// 加载静态资源
	r.LoadHTMLFiles("./views/index.html")
	r.GET("/", func(c *gin.Context) {
		c.HTML(200, "index.html", nil)
	})

	// 使用jwt 验证
	authV1 := r.Group("api/v1")
	authV1.Use(middleware.JwtToken())
	{
		// 用户模块的路由接口
		authV1.PUT("user/:id", v1.EditUser)
		authV1.DELETE("user/:id", v1.DeleteUser)

		//	分类 的路由接口
		authV1.POST("cate/add", v1.AddCate)
		authV1.PUT("cate/:id", v1.EditCate)
		authV1.DELETE("cate/:id", v1.DeleteCate)

		// 文章的路由接口
		authV1.POST("article/add", v1.AddArticle)
		authV1.PUT("article/:id", v1.EditArticle)
		authV1.DELETE("article/:id", v1.DeleteArticle)

		// 上传文件
		authV1.POST("upload", v1.UpLoad)
	}

	// 直接访问
	routerV1 := r.Group("api/v1")
	{
		routerV1.POST("user/add", v1.AddUser)
		routerV1.GET("users", v1.GetUsers)
		routerV1.GET("user/:id", v1.GetUserInfo)

		routerV1.GET("cates", v1.GetCates)
		routerV1.GET("cate/:id", v1.GetCateInfo)

		routerV1.GET("article/list/:id", v1.GetArtByCate)
		routerV1.GET("article/info/:id", v1.GetArtInfo)
		routerV1.GET("articles", v1.GetArticles)
		routerV1.POST("login", v1.Login)
		routerV1.GET("jijin", v1.GetJiJin)
	}

	r.Run(utils.HttpPort)
}
